package com.liukai.bishe.ui.dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.liukai.bishe.R;
import com.liukai.bishe.model.doman.transaction.Transaction;
import com.liukai.bishe.model.doman.transaction.TransactionUseTime;
import com.liukai.bishe.presenter.transactionPresenter.ITransactionPresenter;
import com.liukai.bishe.utils.Constants;
import com.liukai.bishe.utils.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class AddTranscationDialog implements View.OnClickListener {

    private Context mContext;
//    private final AlertDialog.Builder mBuilder;

    private int mTransactionTime = 25; // 事务时间
    private AlertDialog mDialog = null;
    private View mView_dialog = null;
    private ImageView mBtnCancel; // 取消
    private ImageView mBtnSure; // 确定
    private EditText mTransactionTitle; // 事务标题
    private TextView mBtnTime25; // 25分钟
    private TextView mBtnTime35; // 35分钟
    private TextView mBtnTime45; // 45分钟
    private TextView mBtnUserDefine; // 自定义时间

    private String type = "ADD";

    private Transaction mTransaction = new Transaction();
    private TransactionUseTime mTransactionUseTime = new TransactionUseTime();

    private ITransactionPresenter mITransactionPresenter;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 1){
                int time = (int)msg.obj;
                mTransactionTime = time;
                updateUI(time);
            }
        }
    };

    public AddTranscationDialog(Context context, ITransactionPresenter iTransactionPresenter){

        type = "ADD";
        this.mITransactionPresenter = iTransactionPresenter;
        this.mContext = context;
        startDialog(mTransaction);

    }

    public AddTranscationDialog(Context context, ITransactionPresenter iTransactionPresenter, Transaction transaction) {

        type = "UPDATE";
        this.mITransactionPresenter = iTransactionPresenter;
        this.mContext = context;
        this.mTransaction = transaction;
        startDialog(transaction);

    }

    private void startDialog(Transaction transaction){
        mView_dialog = View.inflate(mContext,R.layout.dialog_add_transaction,null);

        if (mDialog == null){
            // 创建AlertDialog对象
            mDialog = new AlertDialog.Builder(mContext).create();
            mDialog.show();
            // 设置点击可取消
            mDialog.setCancelable(true);
            // 获取Window对象
            Window window = mDialog.getWindow();
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            // 设置显示视图内容
            window.setContentView(mView_dialog);

//            WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
//            lp.width = 500;// 调整该值可以设置对话框显示的宽度
//            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//            window.setAttributes(lp);
        }else {
            mDialog.show();
        }

        initView(transaction);
        initListener();

    }

    private void updateUI(int time) {
        mBtnUserDefine.setText(time+"分钟");
    }

    private Handler mHandler;

    private void initListener() {
        mBtnCancel.setOnClickListener(this);
        mBtnSure.setOnClickListener(this);
        mTransactionTitle.setOnClickListener(this);
        mBtnTime25.setOnClickListener(this);
        mBtnTime35.setOnClickListener(this);
        mBtnTime45.setOnClickListener(this);
        mBtnUserDefine.setOnClickListener(this);
    }

    private void initView(Transaction transaction) {

        mBtnCancel = mView_dialog.findViewById(R.id.btnCancel);
        mBtnSure = mView_dialog.findViewById(R.id.btnSure);
        mTransactionTitle = mView_dialog.findViewById(R.id.transactionTitle);
        mBtnTime25 = mView_dialog.findViewById(R.id.btnTime25);
        mBtnTime35 = mView_dialog.findViewById(R.id.btnTime35);
        mBtnTime45 = mView_dialog.findViewById(R.id.btnTime45);
        mBtnUserDefine = mView_dialog.findViewById(R.id.btnUserDefined);

        if (transaction.getId() != null && !transaction.getId().equals("")){
            mTransactionTitle.setText(transaction.getTransactionname());
            String oldTime = transaction.getTime();
            if (oldTime.equals("25") && oldTime != null){
                mTransactionTime = 25;
                mBtnTime25.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_choice));
                mBtnTime25.setTextColor(mContext.getColor(R.color.colorBlue));
                mBtnTime35.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime35.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnTime45.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime45.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnUserDefine.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnUserDefine.setTextColor(mContext.getColor(R.color.colorTextBlack));
            }else if(oldTime.equals("35")){
                mTransactionTime = 35;
                mBtnTime35.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_choice));
                mBtnTime35.setTextColor(mContext.getColor(R.color.colorBlue));
                mBtnTime25.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime25.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnTime45.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime45.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnUserDefine.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnUserDefine.setTextColor(mContext.getColor(R.color.colorTextBlack));
            }else if(oldTime.equals("45")){
                mTransactionTime = 45;
                mBtnTime45.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_choice));
                mBtnTime45.setTextColor(mContext.getColor(R.color.colorBlue));
                mBtnTime35.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime35.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnTime25.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime25.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnUserDefine.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnUserDefine.setTextColor(mContext.getColor(R.color.colorTextBlack));
            }else {
                mTransactionTime = 1;
                mBtnUserDefine.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_choice));
                mBtnUserDefine.setTextColor(mContext.getColor(R.color.colorBlue));
                mBtnTime35.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime35.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnTime45.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime45.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnTime25.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime25.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnUserDefine.setText(transaction.getTime());
            }
        }


    }

    public void show(){
        mDialog.setCanceledOnTouchOutside(false);
//        mDialog.show();
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.btnCancel : // 取消
                mDialog.dismiss();
                break;

            case R.id.btnSure : // 确定

//                Message message = Message.obtain();
//                message.what = 1;
//                message.obj = mTransactionTime;
//                handler.sendMessage(message);
                String title = mTransactionTitle.getText().toString();
                List<Transaction> transactionList = Constants.getTransactionList();
                int i;
                for (i = 0;i < transactionList.size();i ++){
                    if (title.equals(transactionList.get(i).getTransactionname())){
                        ToastUtil.show(mContext,"添加失败：此事务已存在");
                        break;
                    }
                }
                if (i >= transactionList.size()){
                    if (type.equals("ADD")){
                        Transaction transaction = new Transaction();
                        long timeStamp = System.currentTimeMillis();
//                String nowTime = Constants.getNowTime(timeStamp);
                        transaction.setUsername(Constants.getUserInfo().getUsername());
                        transaction.setAddtime(String.valueOf(timeStamp));
                        transaction.setTransactionname(title);
                        transaction.setTime(mTransactionTime+"");
                        transaction.setId(String.valueOf(timeStamp) +
                                Constants.getUserInfo().getUsername());
                        mITransactionPresenter.addTransaction(transaction);
                    }else{
//                        Transaction transaction = new Transaction();
                        long timeStamp = System.currentTimeMillis();
                        mTransaction.setAddtime(String.valueOf(timeStamp));
                        mTransaction.setTransactionname(title);
                        mTransaction.setTime(mTransactionTime+"");
                        ToastUtil.log(mContext,"updateTransactionById",mTransaction.toString());
                        mITransactionPresenter.updateTransactionById(mTransaction);
                    }
                }

                mDialog.dismiss();


                break;

            case R.id.transactionTitle : // 事务标题

                break;

            case R.id.btnTime25 : // 25分钟
                mTransactionTime = 25;
                mBtnTime25.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_choice));
                mBtnTime25.setTextColor(mContext.getColor(R.color.colorBlue));
                mBtnTime35.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime35.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnTime45.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime45.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnUserDefine.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnUserDefine.setTextColor(mContext.getColor(R.color.colorTextBlack));

                break;

            case R.id.btnTime35 : // 35分钟
                mTransactionTime = 35;
                mBtnTime35.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_choice));
                mBtnTime35.setTextColor(mContext.getColor(R.color.colorBlue));
                mBtnTime25.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime25.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnTime45.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime45.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnUserDefine.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnUserDefine.setTextColor(mContext.getColor(R.color.colorTextBlack));

                break;

            case R.id.btnTime45 : // 45分钟
                mTransactionTime = 45;
                mBtnTime45.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_choice));
                mBtnTime45.setTextColor(mContext.getColor(R.color.colorBlue));
                mBtnTime35.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime35.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnTime25.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime25.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnUserDefine.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnUserDefine.setTextColor(mContext.getColor(R.color.colorTextBlack));

                break;

            case R.id.btnUserDefined : // 自定义时间
                mTransactionTime = 1;
                mBtnUserDefine.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_choice));
                mBtnUserDefine.setTextColor(mContext.getColor(R.color.colorBlue));
                mBtnTime35.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime35.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnTime45.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime45.setTextColor(mContext.getColor(R.color.colorTextBlack));
                mBtnTime25.setBackground(mContext.getDrawable(R.drawable.shape_btn_transaction_time));
                mBtnTime25.setTextColor(mContext.getColor(R.color.colorTextBlack));

//                if (mTransaction.getTime() == null)
                UserDefineTimeDialog defineTimeDialog = new UserDefineTimeDialog(mContext,mTransaction.getTime());
                defineTimeDialog.setHandler(handler);
                defineTimeDialog.show();

                break;
        }

    }


//    public void setHandler(Handler handler) {
//        this.mHandler = handler;
//    }
}
